//
//  ViewController.m
//  HTTPS
//
//  Created by J on 16/2/20.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURLSessionConfiguration* confi =[NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    
   NSURLSessionDataTask* task = [[NSURLSession sessionWithConfiguration:confi delegate:self delegateQueue:[NSOperationQueue new]] dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        
    }] ;
    
    [task resume];
}


#pragma mark --NSURLSessionDelegate
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    
    if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {//如果不是服务器信任类型直接返回
        return;
    }
    
//    NSURLSessionAuthChallengeDisposition 处理方式
//    NSURLCredential  证书
    
    if(completionHandler){
        completionHandler(NSURLSessionAuthChallengeUseCredential,challenge.proposedCredential);
    }
    
    
    
}



@end
